﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using SCM.Common.Extensions;
using System;

namespace SCM.Common
{
    /// <summary>
    /// Redis服务
    /// </summary>
    public static class RedisService
    {
        public static IServiceCollection AddRedis(this IServiceCollection services, string connectStr, int dbNum)
        {
            RedisHelper redis = new RedisHelper();
            redis.Init(connectStr, dbNum);
            services.AddSingleton<IRedisHelper>(redis);
            return services;
        }

        private static IServiceProvider ServiceProvider;
        public static void UseStateRedis(this IApplicationBuilder applicationBuilder)
        {
            ServiceProvider = applicationBuilder.ApplicationServices;
        }

        public static IRedisHelper GetRedisHelper()
        {
            return ServiceProvider.GetRequiredService<IRedisHelper>();
        }
    }
}
